// pages/create/create.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    img_url: [],
    content:'',
    imagePath:'',
    rgb: 'rgb(28,28,28)',
    pick: false,
    maskHidden: false,
    name: "",
    fontSize:15,
    array: [10, 11, 12, 13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],
    index: 5,//默认显示位置
  },
  toPick: function () {
    this.setData({
      pick: true
    })
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    /**
     * 获取当前设备的宽高
     */
    wx.getSystemInfo( {

        success: function( res ) {
            that.setData( {
                winWidth: res.windowWidth,
                winHeight: res.windowHeight
            });
        }

    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成   
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
   //  tab切换逻辑
  swichNav: function( e ) {

    var that = this;

    if( this.data.currentTab === e.target.dataset.current ) {
        return false;
    } else {
        that.setData( {
            currentTab: e.target.dataset.current
        })
    }
  },

  bindChange: function( e ) {

    var that = this;
    that.setData( { currentTab: e.detail.current });

  },
  selectImage:function(){
    var that = this;
    wx.chooseImage({
      count: 9, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        if (res.tempFilePaths.length>0){
          //图如果满了9张，不显示加图
          if (res.tempFilePaths.length == 9){
            that.setData({
              hideAdd:1
            })
          }else{
            that.setData({
              hideAdd: 0
            })
          }
          //把每次选择的图push进数组
          let img_url = that.data.img_url;
          for (let i = 0; i < res.tempFilePaths.length; i++) {
            img_url.push(res.tempFilePaths[i])
          }
          that.setData({
            img_url: img_url
          })
          console.log(img_url)
        }
      }
    })
  },
  input:function(e){
    this.setData({
     content:e.detail.value
    })
  },
  // 发布
  save:function(){
    
  },

  //字体选择
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  // 改变字体颜色
  pickColor(e) {
    console.log(e.detail.color)
    this.setData({
      rgb: e.detail.color
    })
  },

  // 切换文字
  switchFont:function(){

  },
  //网络背景
  internetBackg:function(){

  },
  // 本地图片
  nativeBackg:function(){
    wx.chooseImage({
      count:1,
      sizeType:['original'],
      success:res=>{
        this.setData({
          imagePath:res.tempFilePaths[0]
        });
      }
    })
  },
  // 生成卡片
  makeTap:function(){
    var that = this;
    this.setData({
      maskHidden: false
    });
    wx.showToast({
      title: '卡片生成中...',
      icon: 'loading',
      duration: 1000
    });
    setTimeout(function () {
      wx.hideToast()
      that.createNewImg();
      that.setData({
        maskHidden: true
      });
    }, 1000)
  },
  createNewImg: function () {
    var that = this;
    var context = wx.createCanvasContext('mycanvas');
    context.setFillStyle("#fff")
    context.fillRect(0, 0, 350, 600)
    var path = that.data.imagePath;
    console.log(path)
    context.drawImage(path, 0, 0, 350,600 );
    context.draw();
   
    setTimeout(function () {
      wx.canvasToTempFilePath({
        canvasId: 'mycanvas',
        success: function (res) {
          var tempFilePath = res.tempFilePath;
          that.setData({
            imagePath: tempFilePath,
            canvasHidden: true
          });
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }, 200);
  },
  //点击保存到相册
  baocun: function () {
    var that = this
    wx.saveImageToPhotosAlbum({
      filePath: that.data.imagePath,
      success(res) {
        wx.showModal({
          content: '卡片已保存到相册',
          showCancel: false,
          confirmText: '确定',
          confirmColor: '#333',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
              /* 该隐藏的隐藏 */
              that.setData({
                maskHidden: false
              })
            }
          }, fail: function (res) {
            console.log(11111)
          }
        })
      }
    })
  },

  
})